//
//  EatShowViewController.m
//  Delicious2
//
//  Created by dllo on 16/1/7.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "EatShowViewController.h"
#import "UMSocial.h"

@interface EatShowViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UIBarButtonItem *item1;
@property (nonatomic, strong) UIBarButtonItem *item2;
@property (nonatomic, retain) UIBarButtonItem *item3;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, strong) DataBaseHandle *dataBaseHandle;
@property (nonatomic, strong) MBProgressHUD *HUD;

@end

@implementation EatShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.item1 = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"SaveImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(rightCollectionDidPress:)];
    self.item2 = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"HaveSaveImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(rightHaveCollectionDidPress:)];
    self.item3 = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"FenxiangImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStyleDone target:self action:@selector(rightButton:)];
    
    self.navigationItem.title = self.urlTitle;
    
    self.dataBaseHandle = [DataBaseHandle shareDataBase];
    [self.dataBaseHandle openDataBase];
    [self.dataBaseHandle createTable];
    
    self.array = [self.dataBaseHandle selectDataBase:self.urlTitle];
    
    if (self.array.count) {
        self.navigationItem.rightBarButtonItems = @[self.item2, self.item3];
    } else {
        self.navigationItem.rightBarButtonItems = @[self.item1, self.item3];
    }
    [self.dataBaseHandle closeDataBase];

    
    NSURL *url = [NSURL URLWithString:self.urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, -50, self.view.frame.size.width, self.view.frame.size.height + 130)];
    self.view.backgroundColor = [UIColor whiteColor];
    self.webView.delegate = self;
    self.webView.scalesPageToFit = YES;
    //   下拉锁死
    self.webView.scrollView.bounces = NO;
    [self.webView loadRequest:request];
    
    [self.view addSubview:self.webView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self.view nightMode];
    [self.webView nightMode];
    
    self.dataBaseHandle = [DataBaseHandle shareDataBase];
    [self.dataBaseHandle openDataBase];
    [self.dataBaseHandle createTable];
    
    self.array = [self.dataBaseHandle selectDataBase:self.urlTitle];
    
    if (self.array.count) {
        self.navigationItem.rightBarButtonItems = @[self.item2, self.item3];
    } else {
        self.navigationItem.rightBarButtonItems = @[self.item1, self.item3];
    }
    [self.dataBaseHandle closeDataBase];
}

-(void)webViewDidStartLoad:(UIWebView *)webView {
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    self.HUD.labelText = @"正在加载";
//    [self.HUD hide:YES afterDelay:2];
    if (self.HUD) {
        self.HUD.labelText = @"正在加载";
        [self.HUD hide:YES afterDelay:2];
    } else {
        self.HUD.labelText = @"加载失败";
        [self.HUD hide:YES afterDelay:3];
    }
}

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    self.HUD.hidden = YES;
}

#pragma mark - 收藏事件
- (void)rightCollectionDidPress:(id)sender {

    [self.dataBaseHandle openDataBase];
    [self.dataBaseHandle createTable];
    [self.dataBaseHandle insertDataBase:self.urlTitle withImage:self.urlImage withURL:self.urlString withKind:self.urlKind];
    [self.dataBaseHandle closeDataBase];
    
    self.navigationItem.rightBarButtonItem = self.item2;

    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.mode = MBProgressHUDModeText;
    self.HUD.margin = 10;
    self.HUD.cornerRadius = 3;
    self.HUD.labelText = @"收藏成功";
    [self.HUD hide:YES afterDelay:1];
}

- (void)rightHaveCollectionDidPress:(id)sender {
    
    [self.dataBaseHandle openDataBase];
    [self.dataBaseHandle createTable];
    [self.dataBaseHandle deleteDataBase:self.urlTitle];
    [self.dataBaseHandle closeDataBase];

    self.navigationItem.rightBarButtonItem = self.item1;
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.mode = MBProgressHUDModeText;
    self.HUD.margin = 10;
    self.HUD.cornerRadius = 3;
    self.HUD.labelText = @"取消收藏";
    [self.HUD hide:YES afterDelay:1];
}

#pragma mark - 分享按键事件
- (void)rightButton:(id)sender {
    NSLog(@"fenxiang");
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"507fcab25270157b37000010" shareText: self.urlString shareImage:nil shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToWechatSession, nil] delegate:nil];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
